package com.yuxl.common.core.config;

import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactory;
import org.springframework.data.repository.core.RepositoryInformation;
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
import org.springframework.stereotype.Component;

/**
 * PostgreSQL仓库过滤器，用于筛选带有PostgreSqlRepository注解的Repository
 */
public class PostgreSqlRepositoryFilter implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof RepositoryFactorySupport) {
            RepositoryFactorySupport factory = (RepositoryFactorySupport) bean;
            // 如果是PostgreSQL的RepositoryFactory，设置自定义的RepositoryInformation获取方法
            if (factory instanceof R2dbcRepositoryFactory && beanName.contains("postgreSql")) {
                R2dbcRepositoryFactory r2dbcFactory = (R2dbcRepositoryFactory) factory;
                // 这里可以添加额外的配置
            }
        }
        return bean;
    }
}